package com.szeldon.pj.jps.exp;

import com.szeldon.pj.jps.data.store.SBAStore;
import com.szeldon.pj.jps.datastore.SimpleObject;
import com.szeldon.pj.jps.envs.ENVS;
import com.szeldon.pj.jps.qres.AbstractQueryResult;
import com.szeldon.pj.jps.qres.BooleanResult;
import com.szeldon.pj.jps.qres.QResStack;
import com.szeldon.pj.jps.visitor.ASTVisitor;

public class OrBinaryExpression extends BinaryExpression {

	/**
	 * 
	 * @param leftExpression
	 * @param rightExpression
	 */
	public OrBinaryExpression(Expression leftExpression, Expression rightExpression) {
		super(leftExpression, rightExpression);
	}

	@Override
	public int compareTo(SimpleObject object) throws IllegalStateException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getValueAsString() {
		// TODO Auto-generated method stub
		return null;
	}

	public void accept(ASTVisitor astVisitor, ENVS envs, QResStack qResStack, SBAStore store) {
		astVisitor.accept(leftExpression);
		AbstractQueryResult leftResult = qResStack.pop();
		if (!(leftResult instanceof BooleanResult)) {
			throw new IllegalStateException("Left result for Or expression must be Boolean. " + "Currently it's "
					+ leftResult.getClass().getName() + ".");
		}

		astVisitor.accept(rightExpression);
		AbstractQueryResult rightResult = qResStack.pop();
		if (!(rightResult instanceof BooleanResult)) {
			throw new IllegalStateException("Right result for Or expression must be Boolean. " + "Currently it's "
					+ rightResult.getClass().getName() + ".");
		}

		boolean result = ((BooleanResult) leftResult).getBool() || ((BooleanResult) rightResult).getBool();
		qResStack.push(new BooleanResult(result));
	}

}
